/**
 * 用户模块
 */

const express = require('express');
const router = express.Router();

const { addUser, queryUser, deleteUser, updateUser } = require ('../file/user');
const { networkStatus } = require('../utils/constant');

// 新增用户
router.post('/add', async (req, res) => {
    try {
        await addUser(req.body);
        res.send({
            code: 200,
            data: null,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: error,
            msg: networkStatus['500'],
        })
    }
})

// 编辑用户
router.post('/update', async (req, res) => {
    try {
        await updateUser(req.body);
        res.send({
            code: 200,
            data: null,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: error,
            msg: networkStatus['500'],
        })
    }
})

// 删除用户
router.post('/delete', async (req, res) => {
    try {
        console.log(req.query);

        const data = await deleteUser(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: error,
            msg: networkStatus['500'],
        })
    }
})

// 查询用户
router.post('/query', async (req, res) => {
    try {
        const data = await queryUser(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: error,
            msg: networkStatus['500'],
        })
    }
})

module.exports = router;
